<?php

namespace App\Http\Models\Admin;

use Spatie\Permission\Models\Permission;

class Permission_ extends Permission
{
    //
    // protected $table="permission";
    protected $fillable = [
        'name','guard_name'
    ];
    
    public function classify_belongsToMany() 
    {
        //关联的模型类名, 关系字段 
        return $this->belongsToMany('App\Http\Models\Admin\ClassifyPermissions','classify_has_permissions','permission_id','classify_id');
    }

    public function role_belongsToMany() 
    {
        //关联的模型类名, 关系字段 
        return $this->belongsToMany('App\Http\Models\Admin\Role_','role_has_permissions','permission_id','role_id');
    }

    public function hasDetails()
        {
            //关联的模型类名, 关系字段
            return $this->hasOne('App\Http\Models\Admin\PermissionsDetails','permission_id');
        }

        protected static function boot() 
        {
            parent::boot();
    
            // 在删除用户时执行
            static::deleting(function($user) {
                // 删除与用户关联的文章
                $user->hasDetails()->delete();
            });
        }
}
